/**
 * Class exists to drive Employee day simulations
 * 
 * Contributors: Conor Craig
 * 
 * */


package project;

import java.util.ArrayList;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class Main {

	private static project.SimulationStatistics.SimulationTime myTime;
	
	private static SimulationStatistics myStats;
	
	private static ArrayList<Employee> workerList;
	
	//index 0 = manager barrier
	//index N = barrier for team N
	private static ArrayList<CyclicBarrier> barrierList = new ArrayList<CyclicBarrier>();
	
	private static ArrayList<Semaphore> semaphoreList = new ArrayList<Semaphore>();

	private static Manager man;
	
	private static long time;
	
	//3 teams, party of 4
	private static void initBarriers() {
		//team barriers
		for(int i = 0; i <= 3; i++){
			CyclicBarrier tempBarrier = new CyclicBarrier(4);
			barrierList.add(tempBarrier);
		}
	}
	
	//make all simulation semaphores
	private static void initSemaphores() {
		Semaphore tempSema = new Semaphore(1);
		semaphoreList.add(tempSema);
	}
	
	//function fills workerList with employees (1 leader to 3 developers)
	private static void initEmployees() {

		for (int N = 1; N <= 3; N++) {
			Employee tempLead = null;
			for (int M = 1; M <= 3; M++) {
				if( M == 1 ){
					//create one Leader every Nth loop
					//give manager barrrier to leaders
					tempLead = new Employee(semaphoreList.get(0) ,barrierList.get(0), barrierList.get(N), true, man, tempLead, N, M, myStats);
					workerList.add(tempLead);
				}
				// create 3 developers
				//give Team barrier to employees
				Employee tempDev = new Employee( barrierList.get(N), false, man, tempLead, N, M+1, myStats);
				workerList.add(tempDev);

			}
		}
	}
	
	/**
	 * Function drives the simulations
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		//define class variables
		workerList = new ArrayList<Employee>();
		initSemaphores();
		initBarriers();
		myStats = new SimulationStatistics(workerList);
		myTime = myStats.new SimulationTime(540);
		man = new Manager( barrierList.get(0), "Man", myStats);
		initEmployees();
		
		
		myTime.start();
		man.start();
		for(Employee e : workerList){
			e.start();
		}
	}
}
